<template>
  <div class="prediction-card">
    <div class="card-header">
      <img class="avatar" :src="item?.authorVo?.headUrl" alt="Avatar" />
      <div class="info">
        <div class="username">{{ item?.authorVo?.nickname }}</div>
        <div class="badges">
          <span class="badge expert">明星专家</span>
          <span class="badge streak" v-if="item?.authorVo?.continuousRed > 0"
            >{{ item?.authorVo?.continuousRed }}连红</span
          >
        </div>
      </div>
      <div class="accuracy">
        <span class="percentage">{{ (item?.authorVo?.weekWin * 100).toFixed() }}%</span>
        <p class="hit-rate">命中率</p>
      </div>
    </div>
    <div class="card-body">
      <div class="recommendation">
        <span class="match">{{ item?.title }}</span>
      </div>
      <div class="details" v-for="res in item.matchList" :key="res.matchId">
        <span class="league">{{ res.leagueName }}</span> ·
        <span class="time">{{ formatTime(res.matchTime) }}</span>
        <div class="teams">
          <div class="team">{{ res.hostTeamName }}</div>
          <div class="vs">vs</div>
          <div class="team">{{ res.guestTeamName }}</div>
        </div>
      </div>
    </div>
    <div class="card-footer">
      <div>
        <span class="betting">竞彩</span>
        <span class="time">{{ hoursAgoFromTimestamp(item.createdTime) }}小时前</span>
      </div>

      <span class="likes">免费查阅</span>
    </div>
  </div>
</template>

<script setup>
import { defineProps } from 'vue'
import dayjs from 'dayjs'

const formatTime = (num) => {
  return dayjs(num).format('MM-DD HH:mm')
}

const props = defineProps(['item'])
function hoursAgoFromTimestamp(timestamp) {
  // 获取当前时间
  const now = dayjs()

  // 将时间戳转换为 Day.js 对象
  const time = dayjs(timestamp)

  // 计算时间差，单位为小时
  const hoursDifference = now.diff(time, 'hour')

  return hoursDifference
}

// 定义数据和逻辑
</script>

<style scoped lang="scss">
.prediction-card {
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 16px;
  background-color: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  margin-bottom: 16px;
  width: 100%;
  // height: 176px;
  border-radius: 8px 8px 8px 8px;
  border: 1px solid #e2eeff;
  cursor: pointer;
  &:hover {
    box-shadow: 0px 4px 8px 0px rgba(0, 255, 10, 0.25);
    border-radius: 8px 8px 8px 8px;
    border: 1px solid rgba(31, 200, 59, 0.5);
  }

  .card-header {
    display: flex;
    align-items: center;
    margin-bottom: 12px;

    .avatar {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      margin-right: 12px;
    }

    .info {
      flex-grow: 1;

      .username {
        font-size: 16px;
        font-weight: bold;
      }

      .badges {
        display: flex;
        margin-top: 4px;

        .badge {
          font-size: 12px;
          border-radius: 12px;
          padding: 2px 6px;
          margin-right: 4px;

          &.expert {
            background-color: #16b13a;
            color: #fff;
          }

          &.media {
            background-color: #4169e1;
            color: #fff;
          }

          &.streak {
            background: #e95d5d;
            color: #fff;
          }
        }
      }
    }

    .accuracy {
      text-align: right;

      .percentage {
        font-size: 20px;
        color: #f44336;
        font-weight: bold;
      }

      .hit-rate {
        margin: 0;
        font-size: 12px;
        color: #000;
      }
    }
  }

  .card-body {
    margin-bottom: 12px;

    .recommendation {
      font-size: 14px;
      font-weight: bold;
      margin-bottom: 8px;

      .match {
        color: #333;
      }
    }

    .details {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 12px;
      color: #888;
      width: 100%;
      height: 32px;
      background: #f6f8ff;
      border-radius: 4px 4px 4px 4px;
      border: 1px solid #e2eeff;
      padding: 0 10px;
      margin-bottom: 10px;
      .league,
      .time {
        font-size: 12px;
        margin-right: 4px;
        margin-left: 4px;
        overflow: hidden;
        width: 80px;
        white-space: nowrap;
      }
      .league {
        width: 50px;
      }

      .teams {
        display: flex;
        align-items: center;

        .team {
          font-size: 14px;
          margin-right: 8px;
          white-space: nowrap;
          overflow: hidden;
          width: 70px;
          text-overflow: ellipsis;
        }

        .vs {
          font-size: 14px;
          color: #888;
          margin-right: 8px;
        }
      }
    }
  }

  .card-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    color: #888;

    .betting {
      font-size: 12px;
    }

    .time {
      font-size: 12px;
      margin-left: 10px;
    }

    .likes {
      display: flex;
      align-items: center;
      font-size: 12px;
      color: #16b13a;
    }
  }
}
</style>
